package com.logic.modular.game.core.service;

import com.logic.modular.game.battle.info.base.PvpPlayerInfo;
import com.logic.modular.game.battle.info.base.PvpPlayerInfoServiceImpl;
import com.logic.modular.game.core.service.base.PvpMatchEndBaseServiceImpl;
import com.logic.modular.game.core.domain.PvpMatchEnd;
import com.logic.modular.game.player.info.PlayerRankServiceImpl;
import com.logic.modular.gameCore.match.core.domain.CoreMatchEndExp;
import com.logic.modular.gameCore.match.core.service.impl.CoreMatchEndExpServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 经验结算服务
 *
 * @author cxy
 * @since 2024-09-09 11:57:21
 */
@Slf4j
@Service
@RequiredArgsConstructor

public class MatchExpServiceImpl extends PvpMatchEndBaseServiceImpl {
    @Resource
    private CoreMatchEndExpServiceImpl coreMatchEndExpService;
    @Resource
    private PlayerRankServiceImpl playerRankServiceImpl;
    @Resource
    private PvpPlayerInfoServiceImpl pvpPlayerInfoServiceImpl;

    /**
     * 结算常规奖励  exp(经验)|
     */
    public PvpMatchEnd obtainExpReward(Long matchId, Long playerId) {
        PvpPlayerInfo loserInfo = pvpPlayerInfoServiceImpl.get(matchId, playerId);

        PvpMatchEnd pvpMatchEnd = select(matchId, playerId);

        long durationReward = durationReward(loserInfo);
        long killerReward = killerReward(loserInfo);
        long beRuler = beRuler(loserInfo);
        long beOneRuler = beOneRuler(loserInfo);

        pvpMatchEnd.setExpReward1(durationReward);
        pvpMatchEnd.setExpReward2(killerReward);
        pvpMatchEnd.setExpReward3(beRuler);
        pvpMatchEnd.setExpReward4(beOneRuler);

        long winPoint = durationReward + killerReward + beRuler + beOneRuler;
        pvpMatchEnd.setExp(winPoint);
        updateById(pvpMatchEnd);

        playerRankServiceImpl.obtainExpReward(playerId, winPoint);
        return pvpMatchEnd;
    }

    /**
     * 1. 击败玩家奖励
     * 1	击败玩家达到#targetValue,奖励主公经验#value1	1	0	50
     * 2	击败玩家达到#targetValue,奖励主公经验#value1	1	5	100
     * 3	击败玩家达到#targetValue,奖励主公经验#value1	1	10	150
     * 4	击败玩家达到#targetValue,奖励主公经验#value1	1	20	200
     */
    public long killerReward(PvpPlayerInfo loserInfo) {
        List<CoreMatchEndExp> config1 = coreMatchEndExpService.listCache(1);

        int size = loserInfo.getKilledPlayers().size();
        return config1.stream()
                .filter(item -> size >= item.getTargetValue())
                .mapToLong(CoreMatchEndExp::getValue)
                .sum();
    }

    /**
     * 5	我方友军单位<=#1, 一统天下获得主公经验#2
     */
    public long beRuler(PvpPlayerInfo loserInfo) {
        int back = 0;

        int size = loserInfo.getAlliesUidList().size();
        Integer isRuler = loserInfo.getIsRuler();
        if (isRuler == 0) {
            return back;
        }

        CoreMatchEndExp exp5 = coreMatchEndExpService.getCache(5L);
        Integer targetValue = exp5.getTargetValue();
        if (size <= targetValue) {
            back += exp5.getValue();
        }
        return back;
    }

    /**
     * 2. 统一天下奖励()
     * 6	我方友军单位=#1, 一统天下获得主公经验#2
     */
    public long beOneRuler(PvpPlayerInfo loserInfo) {
        int back = 0;

        int size = loserInfo.getAlliesUidList().size();
        Integer isRuler = loserInfo.getIsRuler();
        if (isRuler == 0) {
            return back;
        }

        CoreMatchEndExp exp6 = coreMatchEndExpService.getCache(6L);
        Integer targetValue1 = exp6.getTargetValue();
        if (size == targetValue1) {
            back += exp6.getValue();
        }
        return back;
    }

    /**
     * 征战时长奖励
     */
    public long durationReward(PvpPlayerInfo loserInfo) {
        Long survivalTime = loserInfo.obtainSurvivalTime();
        CoreMatchEndExp exp7 = coreMatchEndExpService.getCache(7L);
        Integer targetValue = exp7.getTargetValue();
        Integer value = exp7.getValue();
        return survivalTime / targetValue * value;
    }
}
